package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Peachblossom;
import com.tanhua.model.mongo.PeachblossomCount;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class PeachblossomApiImpl implements PeachblossomApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    //上传语音
    @Override
    public void save(Peachblossom peachblossom) {
        mongoTemplate.save(peachblossom);
    }
    //查询语音
    @Override
    public Peachblossom find(Long userId) {
        Criteria criteria = Criteria.where("pickkup").is(0)
                                    .and("userId").nin(userId);
        Query query = Query.query(criteria);
        Peachblossom peachblossom = mongoTemplate.findOne(query, Peachblossom.class);
        return peachblossom;
    }

    //查询操作用户今日已拾取数
    @Override
    public PeachblossomCount findCount(Long userId, String date) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("date").is(date);
        Query query = Query.query(criteria);
        return mongoTemplate.findOne(query,PeachblossomCount.class);
    }


    //添加或修改接收次数
    @Override
    public void saveOrUpdateCount(Long userId, String date, int count) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("date").is(date);
        Query query = Query.query(criteria);
        PeachblossomCount peachblossomCount = mongoTemplate.findOne(query, PeachblossomCount.class);
        if (peachblossomCount==null){
            peachblossomCount=new PeachblossomCount();
            peachblossomCount.setCount(count);
            peachblossomCount.setUserId(userId);
            peachblossomCount.setDate(date);
            mongoTemplate.save(peachblossomCount);
        }else{
            Update update=new Update();
            update.set("count",count);
            mongoTemplate.updateFirst(query,update,PeachblossomCount.class);
        }
    }
    //通过此次查询的语音数据并改变pickkup拾取状态
    @Override
    public void updatePickkup(ObjectId id) {
        Criteria criteria = Criteria.where("id").is(id);
        //并将拾取状态改变成已拾取状态
        Query query = Query.query(criteria);
        Update update=new Update();
        update.set("pickkup",1);
        mongoTemplate.updateFirst(query,update,Peachblossom.class);
    }

    //通过此次查询的语音数据并改变pickkup拾取状态====作废
//    @Override
//    public void updatePickkup(Peachblossom peachblossom) {
//        //
//        Criteria criteria = Criteria.where("id").is(peachblossom.getId());
//        //并将拾取状态改变成已拾取状态
//        Query query = Query.query(criteria);
//        Update update=new Update();
//        update.set("pickkup",1);
//        mongoTemplate.updateFirst(query,update,Peachblossom.class);
//    }
}
